/*
 * Jamus
 * Copyright (C) 2010 Jamus Team
 *
 * Jamus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jamus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jamus.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * QHelperThread.cpp
 *
 *  Created on: Feb 3, 2010
 *      Author: christian moellinger
 */

#include "QHelperThread.h"

QHelperThread::QHelperThread(frontend::Plugin& frontend, backend::Plugin& backend) :
			frontend(frontend), backend(backend),
			logger(log4cxx::Logger::getLogger("frontend.amarok.Plugin.QHelperThread"))
{

}

void QHelperThread::run()
{
		QEventLoop loop;
		QDBusConnection connection = QDBusConnection::sessionBus();

		QDBusConnection::sessionBus().registerService("org.kde.amarok");
		QDBusConnection::sessionBus().registerService("org.mpris.amarok");

		QFunctionWrapper *player = new QFunctionWrapper(backend); //TODO: memory leak ;)
		QPlayerAdaptor *test = new QPlayerAdaptor(player);

		QFunctionWrapper *tracklist = new QFunctionWrapper(backend); //TODO: memory leak ;)
		new QTrackListAdaptor(tracklist);

		if (QDBusConnection::sessionBus().registerObject("/Player", player)!=true)
			LOG4CXX_FATAL(logger,"could not register dbus object!");
		if (QDBusConnection::sessionBus().registerObject("/TrackList", tracklist)!=true)
			LOG4CXX_FATAL(logger,"could not register dbus object!");

		LOG4CXX_TRACE(logger,"QHelperThread::run()");

		while (true)
		{
			loop.processEvents();
			if (test!=0)
				test->emitSignals();
			QThread::msleep(50);

		}
}
